﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class ThamSoDAO: AbtractDAO
    {
        public static void UpdateThamSo(long maThamSo, double giaTri)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE THAMSO SET GiaTri=@GiaTri WHERE MaThamSo=@MaThamSo";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@GiaTri", OleDbType.Double);
                thamSo.Value = giaTri;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaThamSo", OleDbType.Integer);
                thamSo.Value = maThamSo;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }

        public static List<ThamSoDTO> LaydsThamSo()
        {
            OleDbConnection ketNoi = null;
            List<ThamSoDTO> dsThamSo = new List<ThamSoDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaThamSo,TenThamSo,GiaTri FROM THAMSO";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    ThamSoDTO ThamSo = new ThamSoDTO();
                    ThamSo.MaThamSo = boLoc.GetInt32(0);

                    if (!boLoc.IsDBNull(1))
                    {
                        ThamSo.TenThamSo = boLoc.GetString(1);
                    }

                    if (!boLoc.IsDBNull(2))
                    {
                        ThamSo.GiaTri = boLoc.GetDouble(2);
                    }

                    dsThamSo.Add(ThamSo);
                }
            }
            catch (Exception ex)
            {
                dsThamSo = new List<ThamSoDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsThamSo;
        }
    }
}
